
package flc.ast.dialog;

import android.content.Context;
import android.view.Gravity;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;

import java.util.ArrayList;
import java.util.List;

import flc.ast.R;
import flc.ast.adapter.ScreenOriAdapter;
import flc.ast.bean.ScreenOriBean;
import flc.ast.databinding.DialogScreenOriBinding;
import stark.common.basic.base.BaseSmartDialog;

public class ScreenOriDialog extends BaseSmartDialog<DialogScreenOriBinding> implements View.OnClickListener {

    private ScreenOriAdapter screenOriAdapter;
    private int selPos = 1;

    @Override
    protected boolean canceledOnTouchOutside() {
        return true;
    }

    @Override
    protected int getGravity() {
        return Gravity.BOTTOM;
    }

    public void setSelPos(int selPos) {
        this.selPos = selPos;
    }

    public ScreenOriDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_screen_ori;
    }

    @Override
    protected void initView(View view) {
        mDataBinding.tvCancel.setOnClickListener(this);
        mDataBinding.tvConfirm.setOnClickListener(this);

        screenOriAdapter = new ScreenOriAdapter();
        mDataBinding.rvList.setLayoutManager(new LinearLayoutManager(getContext()));
        mDataBinding.rvList.setAdapter(screenOriAdapter);
        getData();
        screenOriAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                screenOriAdapter.getItem(selPos).setSelected(false);
                screenOriAdapter.getItem(position).setSelected(true);
                screenOriAdapter.notifyDataSetChanged();
                selPos = position;
            }
        });
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.tvCancel) { //取消
            dismiss();
        } else if (id == R.id.tvConfirm) {  //确认
            if (listener != null) {
                listener.clickConfirm(selPos);
            }
            dismiss();
        }
    }

    private IListener listener;

    public void setListener(IListener listener) {
        this.listener = listener;
    }

    public interface IListener {
        //点击item
        void clickConfirm(int position);
    }

    private void getData() {
        List<ScreenOriBean> list = new ArrayList<>();
        list.add(new ScreenOriBean(R.drawable.yy1, getContext().getString(R.string.screen_ori_text1)));
        list.add(new ScreenOriBean(R.drawable.yy2, getContext().getString(R.string.screen_ori_text2)));
        list.add(new ScreenOriBean(R.drawable.yy3, getContext().getString(R.string.screen_ori_text3)));
        list.add(new ScreenOriBean(R.drawable.yy4, getContext().getString(R.string.screen_ori_text4)));
        list.add(new ScreenOriBean(R.drawable.yy5, getContext().getString(R.string.screen_ori_text5)));
        list.add(new ScreenOriBean(R.drawable.yy6, getContext().getString(R.string.screen_ori_text6)));
        list.add(new ScreenOriBean(R.drawable.yy7, getContext().getString(R.string.screen_ori_text7)));
        list.add(new ScreenOriBean(R.drawable.yy8, getContext().getString(R.string.screen_ori_text8)));
        list.get(selPos).setSelected(true);
        screenOriAdapter.setList(list);
    }

}
